package com.laker.admin.module.inv.controller;

import cn.hutool.core.util.StrUtil;
import com.laker.admin.module.inv.entity.InvMessage;
import com.laker.admin.module.inv.service.IInvMessageService;
import org.springframework.web.bind.annotation.RequestMapping;
import com.laker.admin.module.inv.entity.InvTradein;
import com.laker.admin.module.inv.service.IInvTradeinService;
import org.springframework.web.bind.annotation.RestController;
import com.laker.admin.framework.model.Response;
import com.laker.admin.framework.model.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author laker
 * @since 2024-03-21
 */
@RestController
@RequestMapping("/inv/tradein")
public class InvTradeinController {
    @Autowired
    IInvTradeinService invTradeinService;

    @Autowired
    IInvMessageService invMessageService;

    @GetMapping
    @ApiOperation(value = "分页查询")
    public PageResponse pageAll(@RequestParam(required = false, defaultValue = "1") long page,
                                @RequestParam(required = false, defaultValue = "10") long limit,
                                String keyWord) {
        Page roadPage = new Page<>(page, limit);
        LambdaQueryWrapper<InvTradein> queryWrapper = new QueryWrapper().lambda();

        queryWrapper.and(StrUtil.isNotBlank(keyWord), i -> i.
                like(StrUtil.isNotBlank(keyWord), InvTradein::getCommName, keyWord)
                .or().like(StrUtil.isNotBlank(keyWord), InvTradein::getCommName, keyWord));

        Page pageList = invTradeinService.page(roadPage, queryWrapper);
        return PageResponse.ok(pageList.getRecords(), pageList.getTotal());
    }

    @GetMapping("/getAll")
    @ApiOperation(value = "查询所有")
    public Response getAll(String keyWord) {
        LambdaQueryWrapper<InvTradein> queryWrapper = new QueryWrapper().lambda();
        queryWrapper.and(StrUtil.isNotBlank(keyWord), i -> i.
                like(StrUtil.isNotBlank(keyWord), InvTradein::getCommName, keyWord)
                .or().like(StrUtil.isNotBlank(keyWord), InvTradein::getCommName, keyWord));
        return Response.ok(invTradeinService.getAll(queryWrapper));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public Response get(@PathVariable Long id) {
        return Response.ok(invTradeinService.getById(id));
    }

    @PostMapping("/modifyin")
    @ApiOperation(value = "变更入库数据")
    public Response modifyin(@RequestBody InvTradein param) {
        if (param.getInvTradeinProdSum() < 0) {
            return Response.error("500", "入库数量不能小于0");
        }

        if (param.getInvTradeinProdSum() < param.getInvTradeinModifySum()) {
            long diff = param.getInvTradeinModifySum() - param.getInvTradeinProdSum();

            InvMessage tmp = invMessageService.getById(param.getInvId());
            if (diff > tmp.getInvSum()) {
                return Response.error("500", "变更减少的数量不允许大于总库存");
            }
        }

        return Response.ok(invTradeinService.modifyin(param));
    }
}
